package com.ruoyi.table.service.impl;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.catering.common.seller.context.SellerContextHolder;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.ruoyi.table.domain.DiningTable;
import com.ruoyi.table.domain.dto.DiningTableSaveBatchDTO;
import com.ruoyi.table.enums.DiningTableStatus;
import com.ruoyi.table.mapper.DiningTableMapper;
import com.ruoyi.table.service.DiningTableService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * 预订 ServiceImpl
 *
 * @author Administrator
 * @date 2023/02/09
 */
@Service
public class DiningTableServiceImpl extends ServiceImpl<DiningTableMapper, DiningTable> implements DiningTableService {

    @Override
    public long countUsing(Long areaId) {
        return baseMapper.selectCount(Wrappers.<DiningTable>lambdaQuery().eq(DiningTable::getAreaId, areaId).in(DiningTable::getStatus, 2, 3, 4, 5));
    }

    @Override
    public TableDataInfo<DiningTable> pageDiningTable(DiningTable diningTable, PageQuery pageQuery) {
        Long areaId = diningTable.getAreaId();
        String diningTableNo = diningTable.getDiningTableNo();
        return TableDataInfo.build(baseMapper.selectPage(pageQuery.build(),
            Wrappers.lambdaQuery(DiningTable.class).eq(ObjUtil.isNotNull(areaId), DiningTable::getAreaId, areaId)
                .likeRight(StrUtil.isNotBlank(diningTableNo), DiningTable::getDiningTableNo, diningTableNo)));
    }

    @Override
    public int saveDiningTable(DiningTable diningTable) {
        diningTable.setDiningTableNo(String.format("%s%s", diningTable.getPrefix(), diningTable.getNumber())).setStatus(1).setStyle(DiningTableStatus.EMPTY_DESK.getStyle());
        return baseMapper.insert(diningTable);
    }

    @Override
    public int updateDiningTable(DiningTable diningTable) {
        diningTable.setDiningTableNo(String.format("%s%s", diningTable.getPrefix(), diningTable.getNumber()));
        return baseMapper.updateById(diningTable);
    }

    @Override
    public boolean saveDiningTableBatch(DiningTableSaveBatchDTO dto) {
        List<String> cantUseNums = StrUtil.isNotBlank(dto.getCantUseNums())
            ? Arrays.asList(dto.getCantUseNums().split(",")) : new ArrayList<>();
        String[] cantUseNumsArr = cantUseNums.toArray(new String[0]);
        List<DiningTable> tableEntityList = new ArrayList<>();
        for (int i = dto.getStartNum(); i <= dto.getEndNum(); i++) {
            if (cantUseNums.size() > 0 && StrUtil.containsAny(String.valueOf(i), cantUseNumsArr)) {
                continue;
            }
            DiningTable table = new DiningTable().setSellerId(SellerContextHolder.getRequiredSellerId())
                .setAreaId(dto.getAreaId()).setPrefix(dto.getPrefix()).setNumber(i)
                .setDiningTableNo(String.format("%s%s", dto.getPrefix(), i)).setStatus(1)
                .setStyle(DiningTableStatus.EMPTY_DESK.getStyle()).setSeatNum(dto.getSeatNum())
                .setRemark(dto.getRemark());
            tableEntityList.add(table);
        }
        return baseMapper.insertBatch(tableEntityList);
    }
}
